package com.drinks.yiniang.adapter.user.order;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseMultiItemQuickRCVAdapter;

import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.user.address.DeliverAddressJson;
import com.drinks.yiniang.entity.user.order.OrderDetailJson;
import com.drinks.yiniang.ui.widget.layout.DeliveryProgressLayout;
import com.drinks.yiniang.ui.widget.textview.TagTextView;
import com.drinks.yiniang.utils.AddressUtils;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.listener.CheckDoubleClickListener;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;


import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_ADDRESS;
import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_LOGISTICS;
import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_LIST;
import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_TOTAL;
import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_DELIVERY;
import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_RECOMMEND_TITLE;
import static com.drinks.yiniang.entity.user.order.OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_RECOMMEND;

/**
 * 订单详情adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class OrderDetailAdapter extends BaseMultiItemQuickRCVAdapter<OrderDetailJson.OrderDetailList, BaseViewHolder> {

    private TagTextView mTagTextView;

    private StringBuilder mBuilder;

    //显示退款
    private boolean mShowRefund;

    public OrderDetailAdapter(Context mContext, @Nullable List<OrderDetailJson.OrderDetailList> data) {
        super(data);

        addItemType(ITEM_TYPE_ORDER_DETAIL_ADDRESS, R.layout.item_order_detail_address);
        addItemType(ITEM_TYPE_ORDER_DETAIL_DELIVERY, R.layout.item_order_detail_delivery);
        addItemType(ITEM_TYPE_ORDER_DETAIL_LIST, R.layout.item_order_detail_goods);
        addItemType(ITEM_TYPE_ORDER_DETAIL_TOTAL, R.layout.item_order_detail_total);
        addItemType(ITEM_TYPE_ORDER_DETAIL_LOGISTICS, R.layout.item_order_detail_logistics);
        addItemType(ITEM_TYPE_ORDER_DETAIL_RECOMMEND_TITLE, R.layout.item_order_detail_recommend_title);
        addItemType(ITEM_TYPE_ORDER_DETAIL_RECOMMEND, R.layout.item_home_goods);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderDetailJson.OrderDetailList item) {
        switch (helper.getItemViewType()) {
            case ITEM_TYPE_ORDER_DETAIL_ADDRESS://地址
                if (item.getTempOrderData() == null) {
                    item.setTempOrderData(new OrderDetailJson.OrderDetailData());
                }

                if (item.getTempOrderData().getAddress() == null) {
                    item.getTempOrderData().setAddress(new DeliverAddressJson.DeliverAddressData());
                }

                //收货人
                mBuilder = new StringBuilder(mContext.getString(R.string.text_receiver_colon));
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getTempOrderData().getAddress().getReal_name()));
                helper.setText(R.id.tv_order_detail_username, mBuilder);
                //收货号码
                helper.setText(R.id.tv_order_detail_mobile, StringUtils.getNullEmptyConvert__(item.getTempOrderData().getAddress().getMobile()));
                //收货地址
                mBuilder = new StringBuilder(mContext.getString(R.string.text_deliver_address_colon));
                mBuilder.append(AddressUtils.getDeliverAddress(item.getTempOrderData().getAddress().getRegion_province(), item.getTempOrderData().getAddress().getRegion_city(), item.getTempOrderData().getAddress().getRegion_district()));
                mBuilder.append(" ");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getTempOrderData().getAddress().getAddress()));
                helper.setText(R.id.tv_order_detail_address, mBuilder);

                break;
            case ITEM_TYPE_ORDER_DETAIL_DELIVERY://物流信息(配送员，配送员联系号码)

                break;
            case ITEM_TYPE_ORDER_DETAIL_LIST://商品
                //封面
                GlideUtils.loadSquareImage(mContext, (RatioRoundImageView) helper.getView(R.id.iv_order_detail_goods_cover), item.getCover_url());
                //价格
                mBuilder = new StringBuilder("¥");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getPay_price()));
                helper.setText(R.id.tv_order_detail_goods_price, mBuilder);
                //名称
                helper.setText(R.id.tv_order_detail_goods_name, item.getGoods_name());
                //推荐语
                helper.setText(R.id.tv_order_detail_goods_slogan, item.getRecommend_reason());
                //数量
                mBuilder = new StringBuilder("x");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getNum()));
                helper.setText(R.id.tv_order_detail_goods_num, mBuilder);
                //容积
                mBuilder = new StringBuilder();
                mBuilder.append(StringUtils.judgeGreaterNumber(item.getSingle_capacity(), 0f) ? item.getSingle_capacity() : "");
                mBuilder.append(mBuilder.length() > 0 ? "ml" : "");
                helper.setText(R.id.tv_order_detail_goods_attrs, mBuilder);

                //是否显示退款
                //订单状态 0待支付 1已支付 2已发货 3已送达 4交易完成 5已评价 6已取消 7退款仲裁（退款中）8 moes里的状态 9门店自提 10已退款 11拒绝退款 12缺货取消一品脱订单状态
                if (mShowRefund) {
                    helper.setVisible(R.id.tv_order_detail_return, true);
                } else {
                    helper.setVisible(R.id.tv_order_detail_return, false);
                }

                helper.addOnClickListener(R.id.tv_order_detail_return);
                break;
            case ITEM_TYPE_ORDER_DETAIL_TOTAL://总计
                helper.setText(R.id.tv_order_detail_total_price, item.getTempOrderData() != null ? "¥" + StringUtils.getNullEmptyConvert__(item.getTempOrderData().getPay_amount()) : "¥--");
                break;
            case ITEM_TYPE_ORDER_DETAIL_LOGISTICS://收货地址和配送信息
                if (item.getTempOrderData() == null) {
                    item.setTempOrderData(new OrderDetailJson.OrderDetailData());
                }

                if (item.getTempOrderData().getAddress() == null) {
                    item.getTempOrderData().setAddress(new DeliverAddressJson.DeliverAddressData());
                }

                if (item.getTempOrderData().getExpress() == null) {
                    item.getTempOrderData().setExpress(new OrderDetailJson.OrderExpress());
                }

                //收货人
                mBuilder = new StringBuilder(mContext.getString(R.string.text_receiver_colon));
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getTempOrderData().getAddress().getReal_name()));
                helper.setText(R.id.tv_order_detail_username, mBuilder);
                //收货号码
                helper.setText(R.id.tv_order_detail_mobile, StringUtils.getNullEmptyConvert__(item.getTempOrderData().getAddress().getMobile()));
                //收货地址
                mBuilder = new StringBuilder(mContext.getString(R.string.text_deliver_address_colon));
                mBuilder.append(AddressUtils.getDeliverAddress(item.getTempOrderData().getAddress().getRegion_province(), item.getTempOrderData().getAddress().getRegion_city(), item.getTempOrderData().getAddress().getRegion_district()));
                mBuilder.append(" ");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getTempOrderData().getAddress().getAddress()));
                helper.setText(R.id.tv_order_detail_address, mBuilder);

                final DeliveryProgressLayout ll = helper.getView(R.id.ll_order_detail_progress);

                ll.addStepView(item.getTempOrderData().getExpress().getExpress_json());

                helper.getView(R.id.tv_order_detail_delivery_all).setOnClickListener(new CheckDoubleClickListener() {
                    @Override
                    public void onCheckDoubleClick(View v) {
                        ll.showAllStepView();
                    }
                });
                break;
            case ITEM_TYPE_ORDER_DETAIL_RECOMMEND://推荐
                GlideUtils.loadGoodsList(mContext, (RatioRoundImageView) helper.getView(R.id.iv_home_goods_cover), item.getCover_url());

                //活动tag
                addLabelTag((AutoLineFeedLayout) helper.getView(R.id.ll_home_goods_label), item.getActivity());

                //商品名称
                helper.setText(R.id.tv_home_goods_name, item.getGoods_name());
                //推荐语
                helper.setText(R.id.tv_home_goods_slogan, item.getRecommend_reason());
                //价格
                mBuilder = new StringBuilder();
                mBuilder.append("¥");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getGoods_price()));
                helper.setText(R.id.tv_home_goods_price, mBuilder);

                helper.addOnClickListener(R.id.iv_home_goods_add_cart);
                break;
            default:
                break;
        }
    }

    public void setShowRefund(boolean mShowRefund) {
        this.mShowRefund = mShowRefund;
    }

    private void addLabelTag(AutoLineFeedLayout layout, List<GoodsJson.GoodsActivity> actList) {
        layout.removeAllViews();

        if (CommonUtils.isEmptyList(actList)) {
            layout.setVisibility(View.GONE);

            return;
        }

        layout.setVisibility(View.VISIBLE);

        for (GoodsJson.GoodsActivity data : actList) {
            mTagTextView = new TagTextView(mContext);

            mTagTextView.setTagColor(data.getTag_color());
            mTagTextView.setText(data.getTag_name());

            layout.addView(mTagTextView);
        }
    }
}
